package com.niuwa.p2p.action.borrowinfomg.enterpriseinfomg;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.entity.channel.CataLogFile;
import com.niuwa.p2p.entity.nwfile.NWFile;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.service.channel.ICataLogFileService;
import com.niuwa.p2p.service.channel.ILenderFileCatalogService;
import com.niuwa.p2p.service.filesystem.FileSystem;
import com.niuwa.p2p.vo.channel.ChannelCatalogLenderCatalogVo;
import com.opensymphony.xwork2.ActionContext;

public class EnterpriseVisitFileAction extends EnterpriseCommonAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8267823682227035369L;
	@Autowired
	private ILenderFileCatalogService lenderFileCatalog;
	@Autowired
	private ICataLogFileService catalogFileService;
	// 文件系统
	@Autowired
	private FileSystem fileSystem;
	// 文件ID
	private List<String> fileID;
	
	private String fileId;

	private String catalogNo;
	/**
	 * 保存文件记录到本地数据库 
	 * 
	 * @return
	 */
	@Action("saveEnterpriseVisitFile")
	public String saveChannelVisitFile() {
		String message = "文件上传失败，请重试";
		String[] fileIDs = super.getParams("fileID[]");
		User currentUser = (User) ActionContext.getContext().getSession()
				.get("user");
		if(!this.getCorpCode().isEmpty()){
			catalogFileService.saveCatalogFile(fileIDs,catalogNo,currentUser,this.getCorpCode());
			message = "你已成功上传文件";
		}
		JsonResult result = new JsonResult();
		result.setMsg(message);
		this.setResult(result);
		return NONE;
	}

	/**
	 * 获得所有的外访文件
	 * 
	 * @return
	 */
	@Action("listEnterpriseVisitFileUpload")
	public String listChannelVisitFileUpload() {
		List<ChannelCatalogLenderCatalogVo> channelCatalogLenderCatalog = new ArrayList<ChannelCatalogLenderCatalogVo>();
		channelCatalogLenderCatalog = lenderFileCatalog.querylenderFile(this.getCorpCode());
		setObjectReSult(channelCatalogLenderCatalog);
		return NONE;
	}

	/**
	 * 假删除本地影像文件记录
	 * 
	 * @return
	 */
	@Action("deleteEnterpriseVisitFile")
	public String deleteImgVideo() {
		// 查询文件
		String message = "文件删除失败";
		catalogFileService.updateCatalogFiles(catalogNo,getFileId(),this.getCorpCode());
		message = "你已成功删除文件";
		JsonResult result = new JsonResult();
		result.setMsg(message);
		this.setResult(result);
		return NONE;
	}

	/**
	 * 预览文件
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("showEnterpriseVisitFile")
	public String showChannelVisitFile() throws Exception {
		List<NWFile> files = new ArrayList<NWFile>();
		// 查询文件
		List<CataLogFile> cataLogFiles = new ArrayList<CataLogFile>();
		cataLogFiles = catalogFileService
				.getCatalogFilesByCatalogNo(catalogNo,this.getCorpCode());
		for (CataLogFile c : cataLogFiles) {
			NWFile file = new NWFile();
			file=fileSystem.getDBFileInfo(c.getFileId());
			files.add(file);
		}
		setObjectReSult(files);
		return NONE;
	}

	
	
	public String getCatalogNo() {
		return catalogNo;
	}

	public void setCatalogNo(String catalogNo) {
		this.catalogNo = catalogNo;
	}

	public List<String> getFileID() {
		return fileID;
	}

	public void setFileID(List<String> fileID) {
		this.fileID = fileID;
	}

	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}


}
